#include "common.h"
#include "regs.h"

/*********************************************************************************************************
  寄存器偏移
*********************************************************************************************************/
#define RBR                   0x0
#define THR                   0x0
#define IER                   0x4
#define LCR                   0xc
#define USR                   0x7C

void uart_putc(char c)
{
    //  若 FIFO 不满就填入数据，否则等待
    while (!(read32(UART0_BASE + USR) & (1 << 1)));

    write32(c, UART0_BASE + THR);
}

void uart_getc(char *addr)
{
    *addr = read8(UART0_BASE + RBR);
}

int uart_rx_empty(void)
{
    return (!(read32(UART0_BASE + USR) & (1 << 3)) ? 1 : 0);
}

void uart_rx_int_enable(void)
{
    write32(read32(UART0_BASE + LCR) & ~(1 << 7), UART0_BASE + LCR);
    write32((1 << 0), UART0_BASE + IER);
}

void uart_puts(char *msg)
{
    char c;

    if (!msg) return;

    while ((c = *msg)) {
        uart_putc(c);
        msg++;
    }
}